﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UserGroupData;
using AutoMapper;
using UserGroupWeb.Models;

namespace UserGroupWeb.Controllers
{
    public class PrizeFormatController : Controller
    {

        private IPrizeFormatRepository _repository = new PrizeFormatRepository();

        //
        // GET: /PrizeFormat/

        public ActionResult Index()
        {
            List<PrizeFormatDTO> dtos = _repository.GetAllPrizeFormats();
            var vm = new PrizeFormatIndexModel();
            vm.PrizeFormats = dtos;
            //Mapper.CreateMap<PrizeFormatDTO, PrizeFormatViewModel>();
            //var vms = Mapper.Map<List<PrizeFormatDTO>, List<PrizeFormatViewModel>>(dtos);

            return View(vm);
        }

        //
        // GET: /PrizeFormat/Details/5

        public ActionResult Details(int id)
        {
            PrizeFormatDTO dto = _repository.GetPrizeFormat(id);
            var vm = new PrizeFormatFormModel();
            vm.PrizeFormat = dto;

            return View(vm);
        }

        //
        // GET: /PrizeFormat/Create
        public ActionResult Create()
        {
            var vm = new PrizeFormatFormModel();
            //var vm = new PrizeFormatViewModel();
            return View(vm);
        } 

        //
        // POST: /PrizeFormat/Create
        [HttpPost]
        public ActionResult Create(PrizeFormatFormModel vm)
        //public ActionResult Create(PrizeFormatViewModel vm)
        {
            try
            {
                //Mapper.CreateMap<PrizeFormatViewModel, PrizeFormatDTO>();
                var dto = vm.PrizeFormat;

                _repository.AddNewPrizeFormat(dto);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /PrizeFormat/Edit/5
 
        public ActionResult Edit(int id)
        {
            PrizeFormatDTO dto = _repository.GetPrizeFormat(id);
            var vm = new PrizeFormatFormModel();
            vm.PrizeFormat = dto;
            //Mapper.CreateMap<PrizeFormatDTO, PrizeFormatViewModel>();
            //var vm = Mapper.Map<PrizeFormatDTO, PrizeFormatViewModel>(dtos);
            return View(vm);
        }

        //
        // POST: /PrizeFormat/Edit/5

        [HttpPost]
        //public ActionResult Edit(int id, FormCollection collection)
        //public ActionResult Edit(PrizeFormatViewModel vm)
        public ActionResult Edit(PrizeFormatFormModel vm)
        {
            try
            {
                //Mapper.CreateMap<PrizeFormatViewModel, PrizeFormatDTO>();
                var dto = vm.PrizeFormat;

                _repository.UpdatePrizeFormat(dto);
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /PrizeFormat/Delete/5
 
        public ActionResult Delete(int id)
        {
            PrizeFormatDTO dto = _repository.GetPrizeFormat(id);
            var vm = new PrizeFormatFormModel();
            vm.PrizeFormat = dto;
            return View(vm);
            //Mapper.CreateMap<PrizeFormatDTO, PrizeFormatViewModel>();
            //var vm = Mapper.Map<PrizeFormatDTO, PrizeFormatViewModel>(dto);
            //return View(vm);
        }

        //
        // POST: /PrizeFormat/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                _repository.DeletePrizeFormat(id);
 
                return RedirectToAction("Index");
            }
            catch
            {
                return RedirectToAction("Details", id);
                //return View();
            }
        }
    }
}
